var clickState=true;
$(document).ready(function(){
	$(document).on("click", ".j_delete_pictures", function() {
	      $(this).parents(".j_picture_item:first").remove();
	 });
	$("#close-button").click(function(){
		var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
	});
	$("#btnGoodsList").click(function(){
		choiceGoodsLayerId=layer.open({
			title: "商品选择",
			type: 2,
			shadeClose: true,
			maxmin:true,
			shade: 0.8,
			area: ['950px','80%'],
			content:basePath+"/goods/queryGoodsForChoose.shtml"
		});
	}); 
  layui.use(['upload','layer'],function(){
	  
  layui.upload({
    url: basePath+'/restful/file/upload.shtml', //上传接口
    ext: 'jpg|png|bmp|jpeg',
    method: 'post',
    success: function(res,input){ //上传成功后的回调
      if(res.code=="000000"){
          var id = res.data[0].id;
	      var src=imgUrl+id;
		  $(".one-image").attr("src",src);
		  $(".one-image").attr("data-img",id);
      }else{
      	layer.msg("上传失败")
      }
    }
  });
});
});
function deleteli(e){$(e).parent().parent().remove();}
function deleteImage(e){$(e).parent().remove();}
function delThemeGoodsItem(e){$(e).parent().parent().remove();}

function saveGroupGoodsInfo(){
	var data={};
	var groupId=$("input[name='groupId']").val();
	var customer=$("input[name='customer']").val();
	var isRecommend=$("input[name='isRecommend']:checked").val();
	var content=$("#remark").val();
	//
	if(customer.length==0){
		layer.msg("适应客群不能为空");
		return false;
	}
	if(isRecommend.length==0){
		layer.msg("是否推荐不能为空!");
		return false;
	}
	if(content.length==0){
		layer.msg("商品详情不能为空");
		return false;
	}
	data.groupGoodsId=groupId;
	data.customer=customer;
	data.isRecommend=isRecommend;
	data.desc=content;
	//获取封面图信息
	var coverImage = $(".one-image").attr("data-img");
	data.image = coverImage;
	//获取组货场景
	var scenes=$("input[name='scene']:checked");
	if(scenes==null||scenes==""||scenes.length==0){
		layer.msg("场景不能为空,至少选择一种场景!");
		return false;
	}
	var scene="";
	for(i=0;i<scenes.length;i++){
		if(i==scenes.length-1){
			scene+=$(scenes[i]).val();
		}else{
			scene+=$(scenes[i]).val()+",";
		}
	}
	data.scene = scene;
	//获取组货风格
	var styles=$("input[name='style']:checked");
	if(styles==null||styles==""||styles.length==0){
		layer.msg("场景不能为空,至少选择一种场景!");
		return false;
	}
	var style="";
	for(i=0;i<styles.length;i++){
		if(i==styles.length-1){
			style+=$(styles[i]).val();
		}else{
			style+=$(styles[i]).val()+",";
		}
	}
	data.style = style;
	//获取商品信息
	var goodsInfo = $("#goods_list").children("tr");
	var goodsItemList = new Array();
	for (var i=0;i<goodsInfo.length;i++) {
		var image = $(goodsInfo[i]).find("img").attr("data-img");
		var goodsName = $(goodsInfo[i]).find("input[name='goodsName']").val();
		var price = $(goodsInfo[i]).find("input[name='goodsPrice']").val();
		var goodsId = $(goodsInfo[i]).find("input[name='goodsId']").val();
		var goodsObject={
				coverImage:image,
				goodsName:goodsName,
				goodsPrice:price,
				goodsId:goodsId
		}
		goodsItemList.push(goodsObject);
	}
	if(goodsItemList.length==0){
		layer.msg("请添加组货商品信息!");
		return false;
	}
	data.goodsItemList = goodsItemList;
	if(clickState){
		clickState=false;
		$.ajax({
			url:basePath+"/group/saveGroupGoods.shtml",  
			type:"post",
			data:{groupGoodsVo:JSON.stringify(data)},
			dataType:"JSON",
			success:function(data){
				if(data.code=="000000"){
					layer.msg("操作成功");
					setTimeout(function(){    
						parent.closeGroupGoodsLayer();
					}, 1000);
				}else{
					clickState=true;
					layer.msg(data.msg);
				}
			},error:function(data){
			}
		});
	}
}
//子页面方法
function closeGoodsListLayer(flag,goodsArray){
	layer.close(choiceGoodsLayerId);
	if(flag==1){
		var content="";
		for(i=0;i<goodsArray.length;i++){
			var goodsId = goodsArray[i].id;
			var goodsName = goodsArray[i].goodsName;
			var coverImage = goodsArray[i].coverImage;
			var price = goodsArray[i].price;
			var imageUrl=basePath+"/images/default_goods.jpg";
			if(coverImage != null){
				imageUrl = imgUrl + coverImage;
			}
			content =  "<tr>";
	      	content += 	"<td>";
	        content += 		"<img data-img=\""+coverImage+"\"  src=\""+imageUrl+"\" height=\"50\" width=\"50\" \/>";	                		
	        content += 	"</td>";
	        content += 	"<td>";
	        content += 		'<input type="text" name="goodsName" value="'+goodsName+'" readonly="readonly">';
	        content += 	"</td>";
	        content += 	"<td>";
	        content += 		'<input type="text" name="goodsPrice" value="'+price+'" readonly="readonly">';
	        content += 	"</td>";
	        content += 	"<td>";
	        content += 		"<button class=\"am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only\" onclick=\"delThemeGoodsItem(this)\"><span class=\"am-icon-trash-o\"></span> 删除</button>";
	        content += 	"</td>";
	        content += 	'<input type="hidden" data-id="'+goodsId+'" name="goodsId" value="'+goodsId+'"/>';
	        content += " </tr>";
	        $("#tblGoodsList").append(content);
		}
	}
	
}